package com.jsb.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.jsb.common.config.Const;
import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.JsonUtil;
import com.jsb.common.utils.RewardUtil;
import com.jsb.mapper.AnswerMapper;
import com.jsb.mapper.QuestionMapper;
import com.jsb.mapper.RewardFlowMapper;
import com.jsb.mapper.UserMapper;
import com.jsb.model.Answer;
import com.jsb.model.AnswerExample;
import com.jsb.model.AnswerExample.Criteria;
import com.jsb.model.Question;
import com.jsb.model.QuestionExample;
import com.jsb.model.RewardFlow;
import com.jsb.model.User;

@Service
public class QuestionService{
	
	private static final Logger log = Logger.getLogger(QuestionService.class);
	
	@Resource
	private QuestionMapper questionMapper;
	
	@Resource
	private AnswerMapper answerMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private RewardFlowMapper rewardFlowMapper;
	
	public void addQuestion(Question question) {
		questionMapper.insertSelective(question);
	}
	
	public void delQuestionByUuid(String uuid) {
		questionMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateQuestion(Question question) {
		questionMapper.updateByPrimaryKeySelective(question);
	}
	
	public void updateQuestionByExample(Question question,QuestionExample questionExample){
	    questionMapper.updateByExampleSelective(question,questionExample);
	}
	
	public Question getQuestionByUuid(String uuid) {
		return questionMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Question> getPageQuestion(QuestionExample questionExample) {
		return questionMapper.selectPage(questionExample);
	}
	
	public int countQuestion(QuestionExample questionExample) {
		return questionMapper.countByExample(questionExample);
	}
	
	public List<Question> selectByExample(QuestionExample example){
	    return questionMapper.selectByExample(example);
	}
	/**
	 * 发悬赏
	 * @param question
	 */
	public void updateReward(Question question) {
		String uuid = question.getUuid();
		List<String> phones = getAnswerUser(uuid);    //能得到悬赏的回答人的手机号
		if(null == phones || phones.size()==0){
			return;
		}
		int n = phones.size();
		log.info(n+"人获得问题："+uuid+"的奖励");
		Integer money = question.getMoney();
		String phoneNum = question.getPhoneNum();     //分得谁的钱
		String id = question.getUuid();               //哪次悬赏
		double reward = RewardUtil.reward(money,n);   //每人分多少
		for (String phone : phones) {
			User selectByPrimaryKey = userMapper.selectByPrimaryKey(phone);
			Double balance = selectByPrimaryKey.getBalance();
			User user = new User();
			user.setPhoneNumber(phone);               //得钱人的手机号
			user.setBalance(balance + reward);
			userMapper.updateByPrimaryKeySelective(user);
			log.info("用户："+phone+"获得悬赏奖励："+reward);
			
			RewardFlow rewardFlow = new RewardFlow();
			rewardFlow.setUuid(IdGen.uuid());
			rewardFlow.setCreateDate(new Date());
			rewardFlow.setQuestionId(id);
			rewardFlow.setRewardPn(phoneNum);
			rewardFlow.setGetMoneyPn(phone);
			rewardFlow.setGetMoney(reward);
			rewardFlowMapper.insert(rewardFlow);
			log.info("插入分钱流水表："+JsonUtil.toJson(rewardFlow));
		}
	}
	
	public List<String> getAnswerUser(String qid){
		List<String> phones = new ArrayList<String>();
		AnswerExample example = new AnswerExample();
		Criteria criteria = example.createCriteria();
		criteria.andQuestionUuidEqualTo(qid);
		criteria.andStatusEqualTo(Integer.valueOf(Const.ANSWERINIT));
		List<Answer> answers = answerMapper.selectByExample(example );
		for (Answer answer : answers) {
			String phoneNumber = answer.getPhoneNumber();
			phones.add(phoneNumber);
		}
		return phones;
	}
}
